<?php

	/**
	 * Manage right side of the layout content.
	 *
	 * @package default
	 * @author Rémi Peron
	 **/
	class ContentRightComponent extends Object
	{
		var $uses = array('Batch', 'Ticket', 'User', 'Role');
		var $controller = null;
		var $project_id = null;

		/**
		 * Function execute when loading a controller. Check the controller name and action.
		 *
		 * @return void
		 * @author Rémi Peron
		 **/
	    function startup(&$controller)
	    {
			if (empty($controller->params['projectId']))
				return;
		
			$sidebar = array('controller' => $controller->name, 'action' => $controller->action);
			if ($sidebar['controller'] == 'Documents' && $sidebar['action'] != 'index') {
				return;
			}
			$this->project_id = $controller->params['projectId'];
			

			$model = $controller->modelClass;

			$users = $controller->$model->query("SELECT * FROM `roles` LEFT JOIN users ON ( users.id = user_id ) WHERE project_id =".$this->project_id." AND access = 'yes' ORDER BY status_id");
			$documents = $controller->$model->query("SELECT * FROM `documents` WHERE project_id =".$this->project_id." AND access = 'yes'");
			$nbBatches = $controller->$model->query('SELECT COUNT(*) AS `count` FROM `batches` WHERE `finished` = \'0\' AND `project_id` = '. $this->project_id);
			$nbTicketsInProcess = $controller->$model->query('SELECT COUNT(*) AS `count` FROM `tickets` WHERE `status` = \'in process\' AND `project_id` = '. $this->project_id);
			$nbQualify = $controller->$model->query('SELECT COUNT(*) AS `count` FROM `tickets` WHERE `status` = \'to qualify\' AND `drafted` = \'0\' AND `project_id` = '. $this->project_id);
			$nbTicketsWaiting = $controller->$model->query('SELECT COUNT(*) AS `count` FROM `tickets` WHERE `status` = \'idle\' AND `drafted` = \'0\' AND `project_id` = '. $this->project_id);
			$sidebar['nbTicketsInProcess'] = $nbTicketsInProcess[0][0]['count'];
			$sidebar['nbTicketsWaiting'] = $nbTicketsWaiting[0][0]['count'];
			$sidebar['nbQualify'] = $nbQualify[0][0]['count'];
			$sidebar['nbBatches'] = $nbBatches[0][0]['count'];
			$sidebar['Users'] = $users;
			$sidebar['Documents'] = $documents;
			$controller->set('sidebar', $sidebar);
	    }
	} // END class ContentRight extends Object

?>